import 'package:flutter/material.dart';
import 'package:imooc_flutter_2024/dao/login_dao.dart';
import 'package:imooc_flutter_2024/util/navigator_utils.dart';
import 'package:imooc_flutter_2024/widget/banner_widget.dart';
import 'package:imooc_flutter_2024/widget/local_nav_widget.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  final bannerList = [
    "https://www.devio.org/io/flutter_app/img/banner/100h10000000q7ght9352.jpg",
    "https://o.devio.org/images/fa/cat-4098058__340.webp",
    "https://o.devio.org/images/fa/photo-1601513041797-a79a526a521e.webp",
    "https://o.devio.org/images/other/as-cover.png",
    "https://o.devio.org/images/other/rn-cover2.png",
  ];

  double appbarAlpha = 1;
  get _logoutButton => ElevatedButton(
      onPressed: () {
        LoginDao.logout();
      },
      child: Text('Logout'));
  get _appBar => Opacity(
        opacity: appbarAlpha,
        child: Container(
          height: 80,
          color: Colors.white,
          child: const Center(
            child: Padding(
              padding: EdgeInsets.only(top: 20),
              child: Text('Home Page'),
            ),
          ),
        ),
      );
  get _listView => ListView(
        children: [
          BannerWidget(bannerList: bannerList),
          _logoutButton,
          LocalNavWidget()
        ],
      );
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: Stack(
        children: [
          MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: NotificationListener(
                onNotification: (scrollNotification) {
                  if (scrollNotification is ScrollUpdateNotification &&
                      scrollNotification.depth == 0) {
                    _onScroll(scrollNotification.metrics.pixels);
                  }
                  return false;
                },
                child: _listView,
              )),
          _appBar,
        ],
      ),
      // appBar: AppBar(
      //   // backgroundColor: Colors.blue,
      //   title: const Text('Home Page'),
      //   actions: [_logoutButton],
      // ),
      // body: Column(
      //   children: [BannerWidget(bannerList: bannerList)],
      // ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  void _onScroll(double pixels) {
    double alapha = pixels / 100;
    if (alapha < 0) {
      alapha = 0;
    } else if (alapha > 1) {
      alapha = 1;
    }
    setState(() {
      appbarAlpha = alapha;
    });
  }
}
